<?php 
namespace app\admin\controller;
class Channel extends Common {

	public function index(){
		$tmp = \Ebcms::db() -> select('channel', '*', [
			'ORDER' => [
				'weight' => 'ASC',
				'id' => 'ASC',
			]
		]);
		$channels = $this -> level($tmp);
		$this -> assign('channels', $channels);
		return $this -> html($this -> fetch());
	}

	public function add(){
		if (\Ebcms::isPost()) {
			\Ebcms::db() -> insert('channel', [
				'pid'=>\mylib\Input::post('pid'),
				'type'=>\mylib\Input::post('type'),
				'path'=>\mylib\Input::post('path'),
				'title'=>\mylib\Input::post('title'),
				'keywords'=>\mylib\Input::post('keywords'),
				'description'=>\mylib\Input::post('description'),
				'weight'=>\mylib\Input::post('weight'),
				'status'=>\mylib\Input::post('status'),
				'nav'=>\mylib\Input::post('nav'),
				'target'=>\mylib\Input::post('target'),
				'model_id'=>\mylib\Input::post('model_id'),
				'pagenum'=>\mylib\Input::post('pagenum'),
				'contribute'=>\mylib\Input::post('contribute'),
				'tpl'=>\mylib\Input::post('tpl'),
				'tpl_detail'=>\mylib\Input::post('tpl_detail'),
				'jump'=>\mylib\Input::post('jump'),
				'body'=>\mylib\Input::post('body'),
			]);
			\mylib\Cache::delete('channels');
			return $this -> success('操作成功！', \mylib\Route::buildUrl('/admin/channel/index'));
		}else{
			$type = \mylib\Input::get('type');
			$this -> assign('type', $type);

			$channels = \Ebcms::db() -> select('channel', '*', [
				'ORDER' => [
					'weight' => 'ASC',
					'id' => 'ASC',
				]
			]);
			$this -> assign('channels', $channels);

			$models = \Ebcms::db() -> select('model', '*');
			$this -> assign('models', $models);

			return $this -> html($this -> fetch());
		}
	}

	public function edit(){
		if (\Ebcms::isPost()) {
			\Ebcms::db() -> update('channel', [
				'pid'=>\mylib\Input::post('pid'),
				// 'type'=>\mylib\Input::post('type'),
				'path'=>\mylib\Input::post('path'),
				'title'=>\mylib\Input::post('title'),
				'keywords'=>\mylib\Input::post('keywords'),
				'description'=>\mylib\Input::post('description'),
				'weight'=>\mylib\Input::post('weight'),
				'status'=>\mylib\Input::post('status'),
				'nav'=>\mylib\Input::post('nav'),
				'target'=>\mylib\Input::post('target'),
				'model_id'=>\mylib\Input::post('model_id'),
				'pagenum'=>\mylib\Input::post('pagenum'),
				'tpl'=>\mylib\Input::post('tpl'),
				'tpl_detail'=>\mylib\Input::post('tpl_detail'),
				'jump'=>\mylib\Input::post('jump'),
				'body'=>\mylib\Input::post('body'),
			], [
				'id'=>\mylib\Input::post('id')
			]);
			\mylib\Cache::delete('channels');
			return $this -> success('操作成功！', \mylib\Route::buildUrl('/admin/channel/index'));
		}else{
			$channel = \Ebcms::db() -> get('channel', '*', ['id'=>\mylib\Input::get('id')]);
			$this -> assign('channel', $channel);

			$channels = \Ebcms::db() -> select('channel', '*', [
				'ORDER' => [
					'weight' => 'ASC',
					'id' => 'ASC',
				]
			]);
			$this -> assign('channels', $channels);

			$models = \Ebcms::db() -> select('model', '*');
			$this -> assign('models', $models);
			
			return $this -> html($this -> fetch());
		}
	}

	public function del(){
		if (\Ebcms::isAjax()) {
			$_ids = \Ebcms::db() -> select('content', 'id', [
				'channel_id'=>\mylib\Input::post('id')
			]);
			if ($_ids) {
				\Ebcms::db() -> delete('content', [
					'id' => $_ids
				]);
				\Ebcms::db() -> delete('tag', [
					'content_id' => $_ids
				]);
				\Ebcms::db() -> delete('attr', [
					'content_id' => $_ids
				]);
			}
			\Ebcms::db() -> delete('channel', [
				'id'=>\mylib\Input::post('id'),
			]);
			\mylib\Cache::delete('channels');
			return $this -> success('操作成功！');
		}
	}

	private function level($channels, $pid=0, $level=0){
		$res = [];
		foreach ($channels as $key => $value) {
			$value['_level'] = $level;
			if ($value['pid'] == $pid) {
				$value['subs'] = $this -> level($channels, $value['id'], $level+1);
				$res[] = $value;
			}
		}
		return $res;
	}
}